const canvas = document.getElementById('snowfall');
const ctx = canvas.getContext('2d');

let snowflakes = [];

function createSnowflake() {
    const x = Math.random() * canvas.width;
    const y = -10;
    const size = Math.random() * 3 + 1;

    return {
        x,
        y,
        size,
        speedY: Math.random() * 3 + 1,
        speedX: Math.random() - 0.5
    };
}

function drawSnowflake(snowflake) {
    ctx.beginPath();
    ctx.arc(snowflake.x, snowflake.y, snowflake.size, 0, Math.PI * 2);
    ctx.fillStyle = '#fff';
    ctx.fill();
}

function updateSnowflake(snowflake) {
    snowflake.y += snowflake.speedY;
    snowflake.x += snowflake.speedX;

    if (snowflake.y > canvas.height) {
        snowflake.y = -10;
        snowflake.x = Math.random() * canvas.width;
    }
}

function draw() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);

    snowflakes.forEach(snowflake => {
        drawSnowflake(snowflake);
        updateSnowflake(snowflake);
    });

    requestAnimationFrame(draw);
}

function initialize() {
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;

    snowflakes = [];
    for (let i = 0; i < 100; i++) {
        snowflakes.push(createSnowflake());
    }

    draw();
}

initialize();

// 在窗口大小改变时重新初始化画布
window.addEventListener('resize', initialize);